#import "CTIPTool.h"
#import <resolv.h>
#import <netdb.h>
#import <netinet/ip.h>
#import <net/ethernet.h>
#import <net/if_dl.h>
#import "Reachability.h"
#import <ifaddrs.h>
#import <net/if.h>
#import <netinet/in.h>
#import <arpa/inet.h>
#import <Foundation/Foundation.h>
#define IOS_CELLULAR    @"pdp_ip0"
#define IOS_WIFI        @"en0"
#define IP_ADDR_IPv4    @"ipv4"
#define IP_ADDR_IPv6    @"ipv6"
@implementation CTIPTool
+(NSString *)ct_getIPV4{
    return [CTIPTool getIPv4Address];
}
+(NSString *)ct_getIPV6{
    return [CTIPTool getIPv6Address];
}
+ (NSString *)getIPv4Address {
    struct ifaddrs *interfaces = NULL;
    struct ifaddrs *temp_addr = NULL;
    NSString *address = @"";
    if (getifaddrs(&interfaces) == 0) {
        temp_addr = interfaces;
        while (temp_addr != NULL) {
            if (temp_addr->ifa_addr->sa_family == AF_INET) {
                if (strcmp(temp_addr->ifa_name, "en0") == 0) {
                    char addressBuffer[INET_ADDRSTRLEN];
                    inet_ntop(AF_INET, &(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr), addressBuffer, INET_ADDRSTRLEN);
                    address = [NSString stringWithUTF8String:addressBuffer];
                }
            }
            temp_addr = temp_addr->ifa_next;
        }
    }
    freeifaddrs(interfaces);
    return address;
}
+ (NSString *)getIPv6Address {
    struct ifaddrs *interfaces = NULL;
    struct ifaddrs *temp_addr = NULL;
    NSString *address = @"";
    if (getifaddrs(&interfaces) == 0) {
        temp_addr = interfaces;
        while (temp_addr != NULL) {
            if (temp_addr->ifa_addr->sa_family == AF_INET6) {
                if (strcmp(temp_addr->ifa_name, "en0") == 0) {
                    char addressBuffer[INET6_ADDRSTRLEN];
                    inet_ntop(AF_INET6, &(((struct sockaddr_in6 *)temp_addr->ifa_addr)->sin6_addr), addressBuffer, INET6_ADDRSTRLEN);
                    address = [NSString stringWithUTF8String:addressBuffer];
                }
            }
            temp_addr = temp_addr->ifa_next;
        }
    }
    freeifaddrs(interfaces);
    return address;
}
@end
